package fr.mairie.parc.page.demande;

import java.util.List;

import javax.annotation.Resource;

import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;
import org.apache.click.extras.control.TableInlinePaginator;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.demande.DemandeFields;
import fr.mairie.parc.control.demande.DemandeTable;
import fr.mairie.parc.domain.Demande;
import fr.mairie.parc.domain.Materiel;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.DemandeManager;
import fr.mairie.parc.service.MaterielManager;
import fr.mairie.parc.service.UserManager;


public class ListDemandePage extends BorderPage {

	private static final long serialVersionUID = 1L;

	@Bindable protected DemandeTable resultTable = new DemandeTable("resultTable", this, ViewDemandePage.class);
	@Resource protected DemandeManager demandeManager;
	@Resource protected UserManager userManager;
	@Resource protected MaterielManager materielManager;

	protected DemandeFields fields = new DemandeFields(false);

	private Form form = new Form("form");
	private Table tableDemande = new Table("tableDemande");
	private Table tableMateriel = new Table("tableMateriel");
	private ActionLink editLinkD = new ActionLink("editD", "Edit", this, "onEditClickD");
	private ActionLink editLinkM = new ActionLink("editM", "Edit", this, "onEditClickM");
	private ActionLink deleteLinkD = new ActionLink("deleteD", "Delete", this, "onDeleteClickD");
	private ActionLink deleteLinkM = new ActionLink("deleteM", "Delete", this, "onDeleteClickM");

	public ListDemandePage() {
		addControl(form);
		addControl(tableDemande);
		addControl(tableMateriel);
		addControl(editLinkD);
		addControl(deleteLinkD);
		addControl(editLinkM);
		addControl(deleteLinkM);

		fields.addAll(form);
		form.add(new ImageSubmit("OK", "/assets/images/32x32/accept.png", this,"onSubmit"));
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClicked"));

		tableDemande.setClass(Table.CLASS_ITS);
		tableDemande.setPageSize(5);
		tableDemande.setShowBanner(true);
		tableDemande.setSortable(true);
		tableDemande.setPaginator(new TableInlinePaginator(tableDemande));
		tableDemande.setPaginatorAttachment(Table.PAGINATOR_INLINE);

		tableDemande.addColumn(new Column("idDemande", "ID"));
		tableDemande.addColumn(new Column("numParc", "Numéro de parc"));
		tableDemande.addColumn(new Column("marque", "Marque"));
		tableDemande.addColumn(new Column("modele", "Modéle"));
		tableDemande.addColumn(new Column("date", "Date"));
		tableDemande.addColumn(new Column("batiment", "Batiment"));
		tableDemande.addColumn(new Column("etage", "Etage"));
		tableDemande.addColumn(new Column("salle", "Salle"));
		tableDemande.addColumn(new Column("direction", "Direction"));
		tableDemande.addColumn(new Column("service", "Service"));
		tableDemande.addColumn(new Column("utilisateur", "Utilisateur"));
		tableDemande.addColumn(new Column("typeDemande", "Type de la demande"));

		Column columnD = new Column("Action");
        columnD.setSortable(false);
        ActionLink[] linksD = new ActionLink[]{editLinkD, deleteLinkD};
        columnD.setDecorator(new LinkDecorator(tableDemande, linksD, "idDemande"));
        tableDemande.addColumn(columnD);

        tableDemande.setDataProvider(new DataProvider<Demande>() {
			private static final long serialVersionUID = 1L;
			public Iterable<Demande> getData() {
				List<Demande> res = demandeManager.selectAll();
		    	return res;
		    }
		});

        tableMateriel.setClass(Table.CLASS_ITS);
        tableMateriel.setPageSize(5);
        tableMateriel.setShowBanner(true);
        tableMateriel.setSortable(true);
        tableMateriel.setPaginator(new TableInlinePaginator(tableMateriel));
        tableMateriel.setPaginatorAttachment(Table.PAGINATOR_INLINE);


        tableMateriel.addColumn(new Column("idMateriel", "ID"));
        tableMateriel.addColumn(new Column("type", "Type"));
        tableMateriel.addColumn(new Column("marque", "Marque"));
        tableMateriel.addColumn(new Column("modele", "Modéle"));
        tableMateriel.addColumn(new Column("stock", "Stock"));
        tableMateriel.addColumn(new Column("prix", "Prix"));
        tableMateriel.addColumn(new Column("numParc", "Numéro parc"));
        tableMateriel.addColumn(new Column("etat", "Etat"));
        Column columnM = new Column("Action");
        columnM.setSortable(false);
        ActionLink[] linksM = new ActionLink[]{editLinkM, deleteLinkM};
        columnM.setDecorator(new LinkDecorator(tableMateriel, linksM, "idMateriel"));
        tableMateriel.addColumn(columnM);

        tableMateriel.setDataProvider(new DataProvider<Materiel>() {
			private static final long serialVersionUID = 1L;
			public Iterable<Materiel> getData() {
				List<Materiel> res = materielManager.selectAll();
		    	return res;
		    }
		});

	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
    	demandeManager = (DemandeManager) ctx.getBean("demandeManager");
    	userManager = (UserManager) ctx.getBean("userManager");
    	materielManager = (MaterielManager) ctx.getBean("materielManager");
	}

	public boolean onEditClickD() {
        Integer id = editLinkD.getValueInteger();
        Demande demande = demandeManager.getDemandeById(id);
        if (demande != null) {
            //form.copyFrom(demande);
            fields.setBatimentValue(demande);
            fields.setEtageVlaue(demande);
            fields.setSalleValue(demande);
            fields.setServiceValue(demande);
            fields.setDirectionValue(demande);
            fields.setUtilisateurValue(demande);

        }
        return true;
    }

	public boolean onEditClickM() {
        Integer id = editLinkM.getValueInteger();
        Materiel materiel = materielManager.getMaterielById(id);
        if (materiel != null) {
            //form.copyFrom(demande);
            fields.setMarqueValue(materiel);
            fields.setModeleValue(materiel);

        }
        return true;
    }
}
